'use strict';
angular.module('validationService', [])
    .service('validationService', ['httpWrapService','$q', function (httpWrapService,$q) {

        /**
         * 验证唯一字段是否重复
         *
         * @param params
         * @returns {boolean}
         */
        this.unique = function(params, url){
            var deferred = $q.defer();
            
            // 默认的验证方法value不能为空
            if(!params.value && !url){
                deferred.resolve(true);
                return;
            }
            if(!url) {
            	url = "validation/unique.do";
            }
            httpWrapService.post(url, params).then(
                function(data){
                    if(data.data.success) {
                        deferred.resolve(true);
                    } else {
                        deferred.reject(false);
                    }
                }, function(){
                    deferred.reject(false);
                }
            )
            return deferred.promise;
        }
    }]);